package com.cd.university.config.security.validate.sms;

import com.cd.university.config.security.properties.SecurityProperties;
import com.cd.university.config.security.validate.ValidateCode;
import com.cd.university.config.security.validate.ValidateCodeGenerator;
import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.ServletWebRequest;

/**
 * 短信验证码的生成
 * @author 陈栋
 * @create 2021/9/11 19:35
 */
@Component("smsValidateCodeGenerator")
public class SmsValidateCodeGenerator implements ValidateCodeGenerator {

    @Autowired
    private SecurityProperties securityProperties;

    private Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 生成短信验证码
     * @param request 前端参数
     * @return 具体的验证码类
     */
    @Override
    public ValidateCode generate(ServletWebRequest request) {
        String code = RandomStringUtils.randomNumeric(securityProperties.getCode().getSmsCode().getLength());
        logger.info("生成的短信验证码为：" + code);
        // 短信默认时间设置为5分钟
        int expireIn = securityProperties.getCode().getSmsCode().getExpireIn()*5;
        return new ValidateCode(code,expireIn);
    }
}
